<?php
/**
 * WeipaiController.php
 *
 * @author: Cyw
 * @email: chenyunwen01@bianfeng.com
 * @created: 2015/4/30 14:37
 * @logs:
 *
 */

namespace App\Http\Controllers\Weipai;

use App\Http\Controllers\Controller;
use App\Entities\WeipaiModel;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Http\Request;

class WeipaiController extends Controller
{
    public function index()
    {
        pagecache_start();
        $weipais = [];// WeipaiModel::all()->toArray();
        return view('weipai.index', compact('weipais'));
        pagecache_end();
    }

    public function see(Request $request)
    {
        pagecache_start();
        $url = $request->input('url');
        return view('weipai.see', compact('url'));
        pagecache_end();
    }

    public function ajaxGetVideo(Request $request) {
        $page = $request->input('page', 1);
        $perpage = $request->input('perpage', 10);
        $weipais = WeipaiModel::forPage($page, $perpage)->get()->toArray();
        return success($weipais);
    }

    /**
     * 赞
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function ajaxAddHeart(Request $request) {
        $value = $request->input('value', 1);
        $id = $request->input('id', false);

        $weipai = WeipaiModel::find($id);
        $weipai->heart = $value < 0 ? $weipai->heart - 1 : $weipai->heart + 1;
        $weipai->save();

        return success(array(
            'heart' => $weipai->heart
        ));
    }


    /**
     * 导入sql
     *
     * @return string
     */
    public function import(Request $request)
    {
        $open = $request->input('open', false);

        if ($open) {
            $data = [];
            $i = 0;

            $file = file(__DIR__ . "/weipai.txt");
            foreach ($file as &$line) {

                if (strpos($line, '用户昵称：') !== false) {
                    $data[$i]['nickname'] = str_replace('用户昵称：', '', $line);

                } else if (strpos($line, '用户帐号：') !== false) {
                    $data[$i]['account'] = str_replace(']', '', str_replace('[', '', str_replace('用户帐号：', '', $line)));

                } else {
                    if (trim($line)) {
                        $data[$i]['url'][] = trim($line);
                    }
                }
                if ($line == PHP_EOL) {
                    $i += 1;
                }
            }

            $sql = 'INSERT INTO `weipai` (`id`, `nickname`, `account`, `url`, `dateline`) VALUES ';

            foreach ((array)$data as $key => $value) {

                $nickname = isset( $value['nickname'] ) ? $value['nickname'] : '';
                $account = isset( $value['account'] ) ?  $value['account'] : '';

                foreach ((array)$value['url'] as $url) {
                    $sql .= '(null, "' . $nickname . '", "' . $account . '", "' . $url . '", "'. date('Y-m-d H:i:s', time()) .'"),';
                }
            }
            $sql = rtrim($sql, ',') . ';';

            return $sql;
        } else {
            return '';
        }
    }
}

